package me.zhengjie.modules.inspection.service.impl;

import me.zhengjie.modules.inspection.domain.InspectionItem;
import me.zhengjie.modules.inspection.domain.InspectionTemplate;
import me.zhengjie.modules.inspection.domain.InspectionTemplateItemRel;
import me.zhengjie.modules.inspection.repository.InspectionTemplateItemRelRepository;
import me.zhengjie.modules.inspection.service.InspectionItemService;
import me.zhengjie.modules.inspection.service.InspectionTemplateItemRelService;
import me.zhengjie.modules.system.service.DictDetailService;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.inspection.repository.InspectionTemplateRepository;
import me.zhengjie.modules.inspection.service.InspectionTemplateService;
import me.zhengjie.modules.inspection.service.dto.InspectionTemplateDTO;
import me.zhengjie.modules.inspection.service.dto.InspectionTemplateQueryCriteria;
import me.zhengjie.modules.inspection.service.mapper.InspectionTemplateMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.util.CollectionUtils;

/**
* @author xuyukun
* @date 2019-07-03
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class InspectionTemplateServiceImpl implements InspectionTemplateService {

    @Autowired
    private InspectionTemplateRepository inspectionTemplateRepository;

    @Autowired
    private InspectionTemplateItemRelRepository inspectionTemplateItemRelRepository;

    @Autowired
    private InspectionTemplateItemRelService inspectionTemplateItemRelService;
    @Autowired
    private InspectionItemService inspectionItemService;
    @Autowired
    private InspectionTemplateMapper inspectionTemplateMapper;

    @Autowired
    private DictDetailService dictDetailService;
    @Override
    public Object queryAll(InspectionTemplateQueryCriteria criteria, Pageable pageable){
        Page<InspectionTemplate> page = inspectionTemplateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        PageUtil.toPage(page.map(inspectionTemplateMapper::toDto));
        Iterator<InspectionTemplate> inspectionTemplates= page.iterator();
        while (inspectionTemplates.hasNext()) {
            InspectionTemplate inspectionTemplate = inspectionTemplates.next();
            if(inspectionTemplate.getObjId()!=null){
                inspectionTemplate.setObjName(dictDetailService.findById(Long.valueOf(inspectionTemplate.getObjId())).getLabel());
            }
            if(inspectionTemplate.getTypeId()!=null){
                if(inspectionTemplate.getTypeId()==1){
                    inspectionTemplate.setTypeName("设备类巡检");
                }else if(inspectionTemplate.getTypeId()==2){
                    inspectionTemplate.setTypeName("设施类巡检");
                }else if(inspectionTemplate.getTypeId()==3){
                    inspectionTemplate.setTypeName("管理类巡检");
                }
            }
            List<InspectionTemplateItemRel> inspectionTemplateItemRelList=inspectionTemplateItemRelService.findByTemplateId(inspectionTemplate.getId());
            List<Long> spIds=new ArrayList<>();
            for(InspectionTemplateItemRel inspectionTemplateItemRel:inspectionTemplateItemRelList){
                spIds.add(inspectionTemplateItemRel.getItemId());
            }
            if(!CollectionUtils.isEmpty(spIds)){
                inspectionTemplate.setCheckBoxDatas(inspectionItemService.findByItemIds(spIds));
            }


        }
        return  PageUtil.toPage(page.map(inspectionTemplateMapper::toDto));
    }

    @Override
    public Object queryAll(InspectionTemplateQueryCriteria criteria){
        return inspectionTemplateMapper.toDto(inspectionTemplateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public InspectionTemplateDTO findById(Long id) {
        Optional<InspectionTemplate> inspectionTemplate = inspectionTemplateRepository.findById(id);
        ValidationUtil.isNull(inspectionTemplate,"InspectionTemplate","id",id);
        return inspectionTemplateMapper.toDto(inspectionTemplate.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public InspectionTemplateDTO create(InspectionTemplate resources) {
        return inspectionTemplateMapper.toDto(inspectionTemplateRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(InspectionTemplate resources) {
        Optional<InspectionTemplate> optionalInspectionTemplate = inspectionTemplateRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalInspectionTemplate,"InspectionTemplate","id",resources.getId());

        InspectionTemplate inspectionTemplate = optionalInspectionTemplate.get();

        inspectionTemplateItemRelRepository.deleteByTemplateId(inspectionTemplate.getId());
        // 此处需自己修改
        resources.setId(inspectionTemplate.getId());
        inspectionTemplateRepository.save(resources);

        for(InspectionItem item: resources.getCheckBoxDatas()) {
            InspectionTemplateItemRel rel = new InspectionTemplateItemRel();
            rel.setItemId(item.getId());
            rel.setTemplateId(resources.getId());
            inspectionTemplateItemRelRepository.save(rel);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        inspectionTemplateItemRelRepository.deleteByTemplateId(id);
        inspectionTemplateRepository.deleteById(id);
    }

    @Override
    public List<InspectionTemplate> findByObjectId(Long dictId) {
        return inspectionTemplateRepository.findByObjectId(dictId);
    }
}